<?php

namespace app\controller\merchant;

use app\BaseController;
use app\model\Account;
use app\model\BalanceNotice;
use app\model\CouponBalanceNotice;
use app\model\CouponOrder;
use app\model\Store;
use app\model\UserScoreRecharge;
use think\response\Json;

class Home extends BaseController
{
    public function accountStat(): Json
    {
        $author = getAuthorization();
        $acM = $this->app->make(Account::class);
        $account = $acM->getDetail($author['id']);
        $balanceModel = $this->app->make(Store::class);
        $scoreNumber = $this->app->make(UserScoreRecharge::class);
        $month = strtotime("-1 month", strtotime("-1 day"));
        $month3 = strtotime("-3 month", strtotime("-1 day"));
        $last_day = strtotime("-1 day");
        if ($account->type == 1) {
            //统计未授权的余额
            $balance = $balanceModel->where(['is_del' => 1, 'pid' => $account['supplier_id']])->sum("balance");
            //获取到店员余额
            $info = $acM->where(['is_del' => 1, 'supplier_id' => $account['supplier_id'], 'type' => 3])->sum("balance");
            $balance = bcadd($balance, $info);
            $score = $scoreNumber->where(['state' => 1, 'pid' => $account['supplier_id']])->sum("number");
            //获取近一月的发放数据
            $score_month = $scoreNumber->where([
                ['state', '=', 1],
                ['pid', '=', $account['supplier_id']],
                ['create_time', 'between', [date("Y-m-d 00:00:00", $month), date("Y-m-d 23:59:59", $last_day)]]
            ])->sum("number");
            $score_month3 = $scoreNumber->where([
                ['state', '=', 1],
                ['pid', '=', $account['supplier_id']],
                ['create_time', 'between', [date("Y-m-d 00:00:00", $month3), date("Y-m-d 23:59:59", $last_day)]]
            ])->sum("number");
        } elseif ($account->type == 2) {
            $balance = $balanceModel->where(['is_del' => 1, 'id' => $account['store_id']])->value("balance");
            //获取到店员余额
            $info = $acM->where(['is_del' => 1, 'store_id' => $account['store_id'], 'type' => 3])->sum("balance");
            $balance = bcadd($balance, $info);
            $score = $scoreNumber->where(['state' => 1, 'sid' => $account['store_id']])->sum("number");
            $score_month = $scoreNumber->where([
                ['state', '=', 1],
                ['sid', '=', $account['store_id']],
                ['create_time', 'between', [date("Y-m-d 00:00:00", $month), date("Y-m-d 23:59:59", $last_day)]]
            ])->sum("number");
            $score_month3 = $scoreNumber->where([
                ['state', '=', 1],
                ['sid', '=', $account['store_id']],
                ['create_time', 'between', [date("Y-m-d 00:00:00", $month3), date("Y-m-d 23:59:59", $last_day)]]
            ])->sum("number");
        } else {
            $balance = $account->balance;
            $score = $scoreNumber->where(['state' => 1, 'aid' => $author['id']])->sum("number");
            $score_month = $scoreNumber->where([
                ['state', '=', 1],
                ['aid', '=', $author['id']],
                ['create_time', 'between', [date("Y-m-d 00:00:00", $month), date("Y-m-d 23:59:59", $last_day)]]
            ])->sum("number");
            $score_month3 = $scoreNumber->where([
                ['state', '=', 1],
                ['aid', '=', $author['id']],
                ['create_time', 'between', [date("Y-m-d 00:00:00", $month3), date("Y-m-d 23:59:59", $last_day)]]
            ])->sum("number");
        }
        $coupon_balance = 0;
        $coupon_month = 0;
        $coupon_month3 = 0;
        $coupon_sev = 0;
        $show_coupon = !empty($account['store']) && $account['store']['coupon_switch'] == 1;
        if(!empty($account['store']) && $account['store']['coupon_switch'] == 1){
            //获取卡券消耗记录
            $coupon_balance = $account['store']['coupon_balance'] > 0?bcdiv($account['store']['coupon_balance'],100,2):0;
            $coupon_sev = $this->app->make(CouponOrder::class)->where(['store_id'=>$account['store']['id'],'state'=>1])->sum('cost');
            $coupon_month = $this->app->make(CouponOrder::class)->where([
                ['store_id', '=', $account['store']['id']],
                ['state', '=', 1],
                ['create_time', 'between', [date("Y-m-d 00:00:00", $month), date("Y-m-d 23:59:59", $last_day)]]
            ])->sum('cost');
            $coupon_month3 = $this->app->make(CouponOrder::class)->where([
                ['store_id', '=', $account['store']['id']],
                ['state', '=', 1],
                ['create_time', 'between', [date("Y-m-d 00:00:00", $month3), date("Y-m-d 23:59:59", $last_day)]]
            ])->sum('cost');
        }
        $balance = (float)($balance > 0 ? bcdiv($balance, 100, 2) : 0);
        $score = (float)($score > 0 ? bcdiv($score, 100, 2) : 0);
        $score_month3 = (float)($score_month3 > 0 ? bcdiv($score_month3, 100, 2) : 0);
        $score_month = (float)($score_month > 0 ? bcdiv($score_month, 100, 2) : 0);
        return $this->success(compact('balance', 'score', 'score_month', 'score_month3','show_coupon','coupon_sev','coupon_month','coupon_month3','coupon_balance'));
    }

    public function rechargeStat(): Json|array
    {
        $author = getAuthorization();
        $account = $this->app->make(Account::class)->getDetail($author['id']);
        $noticeModel = $this->app->make(BalanceNotice::class);
        $last_day = strtotime("-1 day");
        $day_7 = strtotime("-7 day", $last_day);
        $month = strtotime("-1 month", $last_day);
        $month3 = strtotime("-3 month", $last_day);
        if ($account->type == 1) {
            $total = $noticeModel->where(['supplier_id' => $account['supplier_id'], 'type' => [1, 3]])->field("sum(number) number,type")->group("type")->select()->toArray();
            $day_7_total = $noticeModel->where([
                ['supplier_id', '=', $account['supplier_id']],
                ['create_time', 'between', [date("Y-m-d 00:00:00", $day_7), date("Y-m-d 23:59:59", $last_day)]],
                ['type', 'in', [1, 3]]
            ])->field("sum(number) number,type")->group("type")->select()->toArray();
            $month_total = $noticeModel->where([
                ['supplier_id', '=', $account['supplier_id']],
                ['create_time', 'between', [date("Y-m-d 00:00:00", $month), date("Y-m-d 23:59:59", $last_day)]],
                ['type', 'in', [1, 3]]
            ])->field("sum(number) number,type")->group("type")->select()->toArray();
            $month3_total = $noticeModel->where([
                ['supplier_id', '=', $account['supplier_id']],
                ['create_time', 'between', [date("Y-m-d 00:00:00", $month3), date("Y-m-d 23:59:59", $last_day)]],
                ['type', 'in', [1, 3]]
            ])->field("sum(number) number,type")->group("type")->select()->toArray();
        } else if ($account->type == 2) {
            $total = $noticeModel->where(['store_id' => $account['store_id'], 'type' => [1, 3]])->field("sum(number) number,type")->group("type")->select()->toArray();
            $day_7_total = $noticeModel->where([
                ['store_id', '=', $account['store_id']],
                ['create_time', 'between', [date("Y-m-d 00:00:00", $day_7), date("Y-m-d 23:59:59", $last_day)]],
                ['type', 'in', [1, 3]]
            ])->field("sum(number) number,type")->group("type")->select()->toArray();
            $month_total = $noticeModel->where([
                ['store_id', '=', $account['store_id']],
                ['create_time', 'between', [date("Y-m-d 00:00:00", $month), date("Y-m-d 23:59:59", $last_day)]],
                ['type', 'in', [1, 3]]
            ])->field("sum(number) number,type")->group("type")->select()->toArray();
            $month3_total = $noticeModel->where([
                ['store_id', '=', $account['store_id']],
                ['create_time', 'between', [date("Y-m-d 00:00:00", $month3), date("Y-m-d 23:59:59", $last_day)]],
                ['type', 'in', [1, 3]]
            ])->field("sum(number) number,type")->group("type")->select()->toArray();
        } else {
            return [];
        }
        $fn = function ($total) {
            $total = array_column($total, 'number', 'type');
            $number = bcsub($total[1] ?? 0, $total[3] ?? 0);
            return $number > 0 ? (float)bcdiv($number, 100, 2) : 0;
        };
        $coupon_balance = 0;
        $coupon_month = 0;
        $coupon_month3 = 0;
        $coupon_sev = 0;
        $show_coupon = !empty($account['store']) && $account['store']['coupon_switch'] == 1;
        if($show_coupon){
            //获取加款
            $coupon_balance = $this->app->make(CouponBalanceNotice::class)->where([
                ['store_id', '=', $account['store']['id']],
                ['type', 'in', [1,3]],
                ['uid','<=',0]
            ])->group('type,number')->field('sum(number) number,type')->select()->toArray();
            $coupon_day_7_total = $this->app->make(CouponBalanceNotice::class)->where([
                ['store_id', '=', $account['store']['id']],
                ['create_time', 'between', [date("Y-m-d 00:00:00", $day_7), date("Y-m-d 23:59:59", $last_day)]],
                ['type', 'in', [1,3]],
                ['uid','<=',0]
            ])->group('type,number')->field('sum(number) number,type')->select()->toArray();
            $coupon_month_total = $this->app->make(CouponBalanceNotice::class)->where([
                ['store_id', '=', $account['store']['id']],
                ['create_time', 'between', [date("Y-m-d 00:00:00", $month), date("Y-m-d 23:59:59", $last_day)]],
                ['type', 'in', [1,3]],
                ['uid','<=',0]
            ])->group('type,number')->field('sum(number) number,type')->select()->toArray();
            $coupon_month3_total = $this->app->make(CouponBalanceNotice::class)->where([
                ['store_id', '=', $account['store']['id']],
                ['create_time', 'between', [date("Y-m-d 00:00:00", $month3), date("Y-m-d 23:59:59", $last_day)]],
                ['type', 'in', [1,3]],
                ['uid','<=',0]
            ])->group('type,number')->field('sum(number) number,type')->select()->toArray();
            $coupon_balance = $fn($coupon_balance);
            $coupon_month = $fn($coupon_month_total);
            $coupon_month3 = $fn($coupon_month3_total);
            $coupon_sev = $fn($coupon_day_7_total);
        }
        $total = $fn($total);
        $day_7_total = $fn($day_7_total);
        $month_total = $fn($month_total);
        $month3_total = $fn($month3_total);
        return $this->success(compact('total', 'day_7_total', 'month_total', 'month3_total','show_coupon','coupon_balance','coupon_month','coupon_month3','coupon_sev'));
    }
}
